﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using IRUM.Core;
using IRUM.XingAPI;
using IRUM.Utils;

namespace IRUM.TR
{
    /// <summary>
    /// 야간선물 기간별투자자매매추이
    /// </summary>
    public class t2814 : CBaseQuery
    {
        public event InvestorTradeHandler OnReceived;
        private List<InvestorTrade> m_list = new List<InvestorTrade>();

        /// <summary>
        /// 야간선물 기간별투자자매매추이
        /// </summary>
        public t2814()
        {
            SetClassData(TR_Type.t2814);
        }

        public void SetOutBlockData(int Index)
        {
            InvestorTrade it = new InvestorTrade();
            it.date = m_Query.GetFieldData(m_OutBlock1Name, "date", Index);
            it.gubun = "D";
            it.time = "290000";
            it.native_sum = Convert.ToInt64(Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "sa_08", Index)));
            it.foreign_sum = Convert.ToInt64(Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "sa_17", Index)));
            it.organization_sum = Convert.ToInt64(Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "sa_18", Index)));

            m_list.Add(it);
        }

        public override void HandleQuery()
        {
            int OccursCount = m_Query.GetBlockCount(m_OutBlock1Name);

            for (int OccursIndex = 0; OccursIndex < OccursCount; OccursIndex++)
            {
                try
                {
                    SetOutBlockData(OccursIndex);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                    continue;
                }
            }

            // 이벤트 발생
            if (OnReceived != null)
            {
                OnReceived(this, new InvestorTradeArgs(m_list));
            }
        }

        protected override void SetRequestData()
        {
            m_Query.SetFieldData(m_InBlockName, "gubun1", 0, "1");                                  //수치구분, 1, 1:수치, 2:누적
            m_Query.SetFieldData(m_InBlockName, "gubun2", 0, "1");                                  //단위구분, 1, 1:일, 2:주, 3:월
            m_Query.SetFieldData(m_InBlockName, "from_date", 0, "20120102");                        //시작날짜, 8, 안넣으면 조회가 이상하게 됨
            m_Query.SetFieldData(m_InBlockName, "to_date", 0, DateTime.Today.ToString("yyyyMMdd")); //종료날짜, 8
        }
    }
}
